package com.google.code.p.restrpc.http;

/**
 * The enum wrapper for org.apache.http.HttpStatus.
 * <p/>
 * Constants enumerating the HTTP status codes.
 * <p/>
 * All status codes defined in RFC1945 (HTTP/1.0), RFC2616 (HTTP/1.1), and
 * RFC2518 (WebDAV) are listed.
 */
public enum HttpStatus {
    
    // --- 1xx Informational ---
    
    /** <tt>100 Continue</tt> (HTTP/1.1 - RFC 2616) */
    CONTINUE(org.apache.http.HttpStatus.SC_CONTINUE, "Continue"),
    /** <tt>101 Switching Protocols</tt> (HTTP/1.1 - RFC 2616) */
    SWITCHING_PROTOCOLS(org.apache.http.HttpStatus.SC_SWITCHING_PROTOCOLS,
                        "Switching Protocols"),
    /** <tt>102 Processing</tt> (WebDAV - RFC 2518) */
    PROCESSING(org.apache.http.HttpStatus.SC_PROCESSING, "Processing"),
    
    // --- 2xx Success ---
    
    /** <tt>200 OK</tt> (HTTP/1.0 - RFC 1945) */
    OK(org.apache.http.HttpStatus.SC_OK, "OK"),
    /** <tt>201 Created</tt> (HTTP/1.0 - RFC 1945) */
    CREATED(org.apache.http.HttpStatus.SC_CREATED, "Created"),
    /** <tt>202 Accepted</tt> (HTTP/1.0 - RFC 1945) */
    ACCEPTED(org.apache.http.HttpStatus.SC_ACCEPTED, "Accepted"),
    /** <tt>203 Non Authoritative Information</tt> (HTTP/1.1 - RFC 2616) */
    NON_AUTHORITATIVE_INFORMATION(org.apache.http.HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION,
                                  "Non-Authoritative Information"),
    /** <tt>204 No Content</tt> (HTTP/1.0 - RFC 1945) */
    NO_CONTENT(org.apache.http.HttpStatus.SC_NO_CONTENT, "No Content"),
    /** <tt>205 Reset Content</tt> (HTTP/1.1 - RFC 2616) */
    RESET_CONTENT(org.apache.http.HttpStatus.SC_RESET_CONTENT, "Reset Content"),
    /** <tt>206 Partial Content</tt> (HTTP/1.1 - RFC 2616) */
    PARTIAL_CONTENT(org.apache.http.HttpStatus.SC_PARTIAL_CONTENT,
                    "Partial Content"),
    /**
     * <tt>207 Multi-Status</tt> (WebDAV - RFC 2518) or <tt>207 Partial Update
     * OK</tt> (HTTP/1.1 - draft-ietf-http-v11-spec-rev-01?)
     */
    MULTI_STATUS(org.apache.http.HttpStatus.SC_MULTI_STATUS, "Multi-Status"),
    
    // --- 3xx Redirection ---
    
    /** <tt>300 Mutliple Choices</tt> (HTTP/1.1 - RFC 2616) */
    MULTIPLE_CHOICES(org.apache.http.HttpStatus.SC_MULTIPLE_CHOICES,
                     "Multiple Choices"),
    /** <tt>301 Moved Permanently</tt> (HTTP/1.0 - RFC 1945) */
    MOVED_PERMANENTLY(org.apache.http.HttpStatus.SC_MOVED_PERMANENTLY,
                      "Moved Permanently"),
    
    /**
     * <tt>302 Moved Temporarily</tt> (Sometimes <tt>Found</tt>) (HTTP/1.0 - RFC
     * 1945)
     */
    MOVED_TEMPORARILY(org.apache.http.HttpStatus.SC_MOVED_TEMPORARILY,
                      "Moved Temporarily"),
    /** <tt>303 See Other</tt> (HTTP/1.1 - RFC 2616) */
    SEE_OTHER(org.apache.http.HttpStatus.SC_SEE_OTHER, "See Other"),
    /** <tt>304 Not Modified</tt> (HTTP/1.0 - RFC 1945) */
    NOT_MODIFIED(org.apache.http.HttpStatus.SC_NOT_MODIFIED, "Not Modified"),
    /** <tt>305 Use Proxy</tt> (HTTP/1.1 - RFC 2616) */
    USE_PROXY(org.apache.http.HttpStatus.SC_USE_PROXY, "Use Proxy"),
    /** <tt>307 Temporary Redirect</tt> (HTTP/1.1 - RFC 2616) */
    TEMPORARY_REDIRECT(org.apache.http.HttpStatus.SC_TEMPORARY_REDIRECT,
                       "Temporary Redirect"),
    
    // --- 4xx Client Error ---
    
    /** <tt>400 Bad Request</tt> (HTTP/1.1 - RFC 2616) */
    BAD_REQUEST(org.apache.http.HttpStatus.SC_BAD_REQUEST, "Bad Request"),
    /** <tt>401 Unauthorized</tt> (HTTP/1.0 - RFC 1945) */
    UNAUTHORIZED(org.apache.http.HttpStatus.SC_UNAUTHORIZED, "Unauthorized"),
    /** <tt>402 Payment Required</tt> (HTTP/1.1 - RFC 2616) */
    PAYMENT_REQUIRED(org.apache.http.HttpStatus.SC_PAYMENT_REQUIRED,
                     "Payment Required"),
    /** <tt>403 Forbidden</tt> (HTTP/1.0 - RFC 1945) */
    FORBIDDEN(org.apache.http.HttpStatus.SC_FORBIDDEN, "Forbidden"),
    /** <tt>404 Not Found</tt> (HTTP/1.0 - RFC 1945) */
    NOT_FOUND(org.apache.http.HttpStatus.SC_NOT_FOUND, "Not Found"),
    /** <tt>405 Method Not Allowed</tt> (HTTP/1.1 - RFC 2616) */
    METHOD_NOT_ALLOWED(org.apache.http.HttpStatus.SC_METHOD_NOT_ALLOWED,
                       "Method Not Allowed"),
    /** <tt>406 Not Acceptable</tt> (HTTP/1.1 - RFC 2616) */
    NOT_ACCEPTABLE(org.apache.http.HttpStatus.SC_NOT_ACCEPTABLE,
                   "Not Acceptable"),
    /** <tt>407 Proxy Authentication Required</tt> (HTTP/1.1 - RFC 2616) */
    PROXY_AUTHENTICATION_REQUIRED(org.apache.http.HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED,
                                  "Proxy Authentication Required"),
    /** <tt>408 Request Timeout</tt> (HTTP/1.1 - RFC 2616) */
    REQUEST_TIMEOUT(org.apache.http.HttpStatus.SC_REQUEST_TIMEOUT,
                    "Request Timeout"),
    /** <tt>409 Conflict</tt> (HTTP/1.1 - RFC 2616) */
    CONFLICT(org.apache.http.HttpStatus.SC_CONFLICT, "Conflict"),
    /** <tt>410 Gone</tt> (HTTP/1.1 - RFC 2616) */
    GONE(org.apache.http.HttpStatus.SC_GONE, "Gone"),
    /** <tt>411 Length Required</tt> (HTTP/1.1 - RFC 2616) */
    LENGTH_REQUIRED(org.apache.http.HttpStatus.SC_LENGTH_REQUIRED,
                    "Length Required"),
    /** <tt>412 Precondition Failed</tt> (HTTP/1.1 - RFC 2616) */
    PRECONDITION_FAILED(org.apache.http.HttpStatus.SC_PRECONDITION_FAILED,
                        "Precondition Failed"),
    /** <tt>413 Request Entity Too Large</tt> (HTTP/1.1 - RFC 2616) */
    REQUEST_TOO_LONG(org.apache.http.HttpStatus.SC_REQUEST_TOO_LONG,
                     "Request Too Long"),
    /** <tt>414 Request-URI Too Long</tt> (HTTP/1.1 - RFC 2616) */
    REQUEST_URI_TOO_LONG(org.apache.http.HttpStatus.SC_REQUEST_URI_TOO_LONG,
                         "Request-URI Too Long"),
    /** <tt>415 Unsupported Media Type</tt> (HTTP/1.1 - RFC 2616) */
    UNSUPPORTED_MEDIA_TYPE(org.apache.http.HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE,
                           "Unsupported Media Type"),
    /** <tt>416 Requested Range Not Satisfiable</tt> (HTTP/1.1 - RFC 2616) */
    REQUESTED_RANGE_NOT_SATISFIABLE(org.apache.http.HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE,
                                    "Requested range not satisfiable"),
    /** <tt>417 Expectation Failed</tt> (HTTP/1.1 - RFC 2616) */
    EXPECTATION_FAILED(org.apache.http.HttpStatus.SC_EXPECTATION_FAILED,
                       "Expectation Failed"),
    
    /**
     * Static constant for a 419 error.
     * <tt>419 Insufficient Space on Resource</tt> (WebDAV -
     * draft-ietf-webdav-protocol-05?) or
     * <tt>419 Proxy Reauthentication Required</tt> (HTTP/1.1 drafts?)
     */
    INSUFFICIENT_SPACE_ON_RESOURCE(org.apache.http.HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE,
                                   "Insufficient Space On Resource"),
    /**
     * Static constant for a 420 error. <tt>420 Method Failure</tt> (WebDAV -
     * draft-ietf-webdav-protocol-05?)
     */
    METHOD_FAILURE(org.apache.http.HttpStatus.SC_METHOD_FAILURE,
                   "Method Failure"),
    
    /** <tt>422 Unprocessable Entity</tt> (WebDAV - RFC 2518) */
    UNPROCESSABLE_ENTITY(org.apache.http.HttpStatus.SC_UNPROCESSABLE_ENTITY,
                         "Unprocessable Entity"),
    /** <tt>423 Locked</tt> (WebDAV - RFC 2518) */
    LOCKED(org.apache.http.HttpStatus.SC_LOCKED, "Locked"),
    /** <tt>424 Failed Dependency</tt> (WebDAV - RFC 2518) */
    FAILED_DEPENDENCY(org.apache.http.HttpStatus.SC_FAILED_DEPENDENCY,
                      "Failed Dependency"),
    
    // --- 5xx Server Error ---
    
    /** <tt>500 Server Error</tt> (HTTP/1.0 - RFC 1945) */
    INTERNAL_SERVER_ERROR(org.apache.http.HttpStatus.SC_INTERNAL_SERVER_ERROR,
                          "Internal Server Error"),
    /** <tt>501 Not Implemented</tt> (HTTP/1.0 - RFC 1945) */
    NOT_IMPLEMENTED(org.apache.http.HttpStatus.SC_NOT_IMPLEMENTED,
                    "Not Implemented"),
    /** <tt>502 Bad Gateway</tt> (HTTP/1.0 - RFC 1945) */
    BAD_GATEWAY(org.apache.http.HttpStatus.SC_BAD_GATEWAY, "Bad Gateway"),
    /** <tt>503 Service Unavailable</tt> (HTTP/1.0 - RFC 1945) */
    SERVICE_UNAVAILABLE(org.apache.http.HttpStatus.SC_SERVICE_UNAVAILABLE,
                        "Service Unavailable"),
    /** <tt>504 Gateway Timeout</tt> (HTTP/1.1 - RFC 2616) */
    GATEWAY_TIMEOUT(org.apache.http.HttpStatus.SC_GATEWAY_TIMEOUT,
                    "Gateway Timeout"),
    /** <tt>505 HTTP Version Not Supported</tt> (HTTP/1.1 - RFC 2616) */
    HTTP_VERSION_NOT_SUPPORTED(org.apache.http.HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED,
                               "HTTP Version not supported"),
    
    /** <tt>507 Insufficient Storage</tt> (WebDAV - RFC 2518) */
    INSUFFICIENT_STORAGE(org.apache.http.HttpStatus.SC_INSUFFICIENT_STORAGE,
                         "Insufficient Storage");
    
    private final int value;
    
    private final String description;
    
    private HttpStatus(int value, String description) {
        this.value = value;
        this.description = description;
    }
    
    /**
     * Return status code.
     */
    public int value() {
        return this.value;
    }
    
    /**
     * Return the description of status code.
     */
    public String getDescription() {
        return description;
    }
    
    @Override
    public String toString() {
        return Integer.toString(value);
    }
    
    public static HttpStatus valueOf(int statusCode) {
        for (HttpStatus status : values()) {
            if (status.value == statusCode) {
                return status;
            }
        }
        throw new IllegalArgumentException("No matching status code for [" + statusCode
                                           + "]");
    }
    
}
